<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/stuWelcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>英语版块</el-breadcrumb-item>
      <el-breadcrumb-item >英语角</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="10">
      <!--搜索框-->
      <el-col :span="6">
        <el-input placeholder="按id查询" v-model="searchId" class="input-with-select"
                  @keyup.enter.native="searchData1">
          <el-button slot="append" icon="el-icon-search" @click="searchdata1"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6"style="margin-left: 107px;">
        <el-input placeholder="按类型查询" v-model="searchType" class="input-with-select"
                  @keyup.enter.native="searchData2">
          <el-button slot="append" icon="el-icon-search" @click="searchdata2"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <!--表单模块-->
    <el-table :data="englishes" highlight-current-row
              ref='table' border stripe height="300" style="margin-top: 30px;">
      <el-table-column type="index" align="center"></el-table-column>
      <el-table-column prop="englishId" label="编号" align="center" width="80"></el-table-column>
      <el-table-column prop="englishTitle" label="题目" align="center" width="130"></el-table-column>
      <el-table-column prop="englishWord" label="英文标题" align="center" width="140"></el-table-column>
      <el-table-column prop="englishType" label="类型" align="center" width="130"></el-table-column>
      <el-table-column prop="englishIntroduce" label="介绍" align="center" ></el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="80">
        <template slot-scope="scope">
          <!-- scope.row 可以取到当前一行的数据 -->
          <!-- {{ scope.row }} -->
          <el-switch v-model="scope.row.status "   active-color="#13ce66"
                     inactive-color="#ff4949"
                     :active-value="1" :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="100">
        <template slot-scope="scope">
          <!-- 详细  阅读 -->
          <el-tag
            v-if="scope.row.status == 0"
            type="primary"
            effect="dark"
            @click="detail(scope.row)"
          >阅读
          </el-tag>
          <el-tag
            v-else
            type="success"
            effect="dark"
          >已阅
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!--分页插件-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="[2, 4, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>
  </el-card>
</template>

<script>
  import axios from 'axios';//axios
  import Qs from 'qs';//qs
  export default {
    name:"englishList",
    //绑定数据
    data() {
      return {
        englishes:[],//注意使用表格时，pop属性对应的内容等价于后端数据传输的字段(键)
        curpage:1,//当前页
        pagesize:4,//每页显示条数
        total:0,//总条数
        searchId:"",
        searchType:"",
      }
    },
    //文档加载事件,当没有任何按钮去点击触发事件时，可以使用该函数在加载时，触发事件！
    mounted() {
      this.englishList();
    },
    methods:{
      detail(data){
        console.log(data);
        // 跳转至阅读页面
        let englishTitle = data.englishTitle;
        console.log(englishTitle);
        switch (englishTitle) {
          case "我爱你": this.$router.replace("/love");break;
          case "好心境": this.$router.replace("/goodMood");break;
          case "坚持梦想，是唯一的选择": this.$router.replace("/dream");break;
          case "热爱生活": this.$router.replace("/life");break;
          case "我所追求的生活": this.$router.replace("/desiredLife");break;
          case "永远的朋友": this.$router.replace("/friend");break;
          case "妈妈给你最好的忠告": this.$router.replace("/counsel");break;
          case "青蛙王子1": this.$router.replace("/frog1");break;
          case "青蛙王子2": this.$router.replace("/frog2");break;
          case "莴苣姑娘1": this.$router.replace("/girl1");break;
          case "莴苣姑娘2": this.$router.replace("/girl2");break;
          case "莴苣姑娘3": this.$router.replace("/girl3");break;
        }
        // 修改状态 发起请求修改保持数据库status字段一致
        data.status = 1;
        axios({
          headers:{'Content-Type':'application/x-www-form-urlencoded'},
          method:"post",
          url:"http://localhost:1313/english/englishUpdate",
          data:Qs.stringify({englishId:data.englishId,status:data.status})
        }).then((res)=>{
          console.log(res)
          if (res.data.code == 200) {
            this.englishList();
          }else{
            this.$message.error(res.data.msg);
          }
        })
      },
      selectRow(row) { // 给操作行 高亮
        if (row) {
          this.$refs.table.setCurrentRow(row, true);  // 高亮某一行
        } else {
          this.$refs.table.setCurrentRow();  // 取消高亮
        }
      },
      //执行分页查询的函数
      englishList(curpage = 1) {
        var englishes=this;
        axios.get(
          "http://localhost:1313/english/queryEnglishByTypePage",
          {params:{current:englishes.curpage,rows:englishes.pagesize,search:englishes.searchType}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.englishes = res.data.data.englishes;
          this.total = res.data.data.total;
        });
      },
      // 监听 pageSize 改变的事件
      handleSizeChange (newSize) {
        //   console.log(newSize)
        //  将监听接受到的每页显示多少条的数据 newSzie 赋值给 pagesize
        this.pagesize = newSize;
        //  修改完以后，重新发起请求获取一次数据
        this.englishList();
      },
      // 监听 页码值  改变的事件
      handleCurrentChange (newPage) {
        //   console.log(newPage)
        //  将监听接受到的页码值的数据 newPage 赋值给 pagenum
        this.curpage = newPage;
        //  修改完以后，重新发起请求获取一次数据
        this.englishList();
      },
      // 根据id完成查询
      englishList1(){
        var englishes=this;
        axios.get(
          "http://localhost:1313/english/queryEnglishByIdPage",
          {params:{current:englishes.curpage, rows: englishes.pagesize,englishId:englishes.searchId}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.englishes = res.data.data.englishes;
          this.total = res.data.data.total;
        });
      },
      searchdata1(){
        this.curpage = 1;
        this.englishList1();
        this.searchId ="";
      },
      // 按下回车键实现搜索
      searchData1(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata1();
        }
      },
      // 按照文章类型
      englishList2(){
        var englishes=this;
        axios.get(
          "http://localhost:1313/english/queryEnglishByTypePage",
          {params:{current:englishes.curpage, rows: englishes.pagesize,search:englishes.searchType}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.englishes = res.data.data.englishes;
          this.total = res.data.data.total;
        });
      },
      searchdata2(){
        this.curpage = 1;
        this.englishList2();
        this.searchType ="";
      },
      // 按下回车键实现搜索
      searchData2(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata2();
        }
      },
    }
  }

</script>

<style>
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color:  rgba(227, 189, 241, 0.76) !important;
    color: #fff;
  }
  .el-input--suffix .el-input__inner {
    padding-right: 15px;
  }
  .form{
    display: grid;
    justify-content: center;
    /* align-items: center; */
    justify-items: center;
  }
  .el-button--info.is-plain {
    color: #704141;
    background: #f4f4f5;
    border-color: #d3d4d6;
  }
  .el-button--mini {
    font-size: 14px;
    border-radius: 3px;
  }
  .el-breadcrumb{
    padding-bottom: 20px;
  }
  .el-pagination{
    padding-top: 20px;
  }
</style>
